package com.css.zfzx.data.primary.qhProbabilityEarthquakeDangerPG.repository.impl;


import com.css.zfzx.data.primary.qhProbabilityEarthquakeDangerPG.repository.QhProbabilityEarthquakeDangerPGNativeRepository;
import com.css.zfzx.data.primary.qhProbabilityEarthquakeDangerPG.repository.entity.QhProbabilityEarthquakeDangerPGEntity;
import com.css.zfzx.data.util.DateUtil;
import jdk.nashorn.internal.objects.annotations.Property;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Repository;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;

/**
 * @author lxg
 * @date 2020-11-10
 */
@Repository
@PropertySource("classpath:platform-config.yml")
public class QhProbabilityEarthquakeDangerPGNativeRepositoryImpl implements QhProbabilityEarthquakeDangerPGNativeRepository {
    @PersistenceContext
    private EntityManager em;

    private static final Logger log = LoggerFactory.getLogger(QhProbabilityEarthquakeDangerPGNativeRepositoryImpl.class);

    @Value("${jpa.limit.size}")
    Integer size;

    @Value("${jpa.limit.num}")
    Integer num;
    @Override
    public List<String> findAreaCreate(LocalDateTime createTime) {
        StringBuilder sql = new StringBuilder("select area from qh_probability_earthquake_danger ");
        StringBuilder whereSql = new StringBuilder(" where is_valid = 1 ");
        if (createTime!=null) {
            whereSql.append(" and create_time >:createTime");
        }else {
            whereSql.append(" and create_time is not null");
        }
        whereSql.append(" and update_time is null");
        whereSql.append(" group by province,city,area");
        Query query = this.em.createNativeQuery(sql.append(whereSql).toString());
        if (createTime!=null) {
            query.setParameter("createTime", createTime);
        }
        List<String> list = query.getResultList();
        return list;
    }

    @Override
    public List<QhProbabilityEarthquakeDangerPGEntity> findEntityCreate(LocalDateTime createTime, String area) {
        StringBuilder sql = new StringBuilder("select uuid,province,city,area,affiliated_file_name,A,B,C,D,E,F,G,H,I," +
                "J,L,M,N,O,P,Q,object_code,is_valid,partition_flag,create_user,create_time,update_user,update_time," +
                "ST_AsText(geo_point) as geo_point,geo_multipoint" +
                " from qh_probability_earthquake_danger ");
        StringBuilder whereSql = new StringBuilder(" where is_valid = 1 ");
        if (createTime!=null) {
            whereSql.append(" and create_time >:createTime");
        }else {
            whereSql.append(" and create_time is not null");
        }
        if (StringUtils.isNotEmpty(area)&&!"null".equals(area)){
            whereSql.append(" and area =:area");
        }
        whereSql.append(" and update_time is null");
        whereSql.append(" order by province,city,area");
        // whereSql.append(" order by create_time");
        // whereSql.append(" limit ").append(size).append(",").append(num);
        Query query = this.em.createNativeQuery(sql.append(whereSql).toString(), QhProbabilityEarthquakeDangerPGEntity.class);
        if (createTime!=null) {
            query.setParameter("createTime", createTime);
        }
        if (area!=null) {
            query.setParameter("area", area);
        }
        List<QhProbabilityEarthquakeDangerPGEntity> list = query.getResultList();
        return list;
    }

    @Override
    public List<String> findAreaUpdate(LocalDateTime updateTime) {
        StringBuilder sql = new StringBuilder("select area from qh_probability_earthquake_danger ");
        StringBuilder whereSql = new StringBuilder(" where 1=1 ");
        if (updateTime!=null) {
            whereSql.append(" and update_time >:updateTime");
        }else {
            whereSql.append(" and update_time is not null");
        }
        whereSql.append(" group by province,city,area");
        Query query = this.em.createNativeQuery(sql.append(whereSql).toString());
        if (updateTime!=null) {
            query.setParameter("updateTime", updateTime);
        }
        List<String> list = query.getResultList();
        return list;
    }

    @Override
    public List<QhProbabilityEarthquakeDangerPGEntity> findEntityUpdate(LocalDateTime updateTime, String area) {
        StringBuilder sql = new StringBuilder("select uuid,province,city,area,affiliated_file_name,A,B,C,D,E,F,G,H,I," +
                "J,L,M,N,O,P,Q,object_code,is_valid,partition_flag,create_user,create_time,update_user,update_time," +
                "ST_AsText(geo_point) as geo_point,geo_multipoint" +
                " from qh_probability_earthquake_danger ");
        StringBuilder whereSql = new StringBuilder(" where 1=1 ");
        if (updateTime!=null) {
            whereSql.append(" and update_time >:updateTime");
        }else {
            whereSql.append(" and update_time is not null");
            whereSql.append(" and is_valid=1");
        }
        if (StringUtils.isNotEmpty(area)&&!"null".equals(area)){
            whereSql.append(" and area =:area");
        }
        whereSql.append(" order by province,city,area");
        Query query = this.em.createNativeQuery(sql.append(whereSql).toString(), QhProbabilityEarthquakeDangerPGEntity.class);
        if (updateTime!=null) {
            query.setParameter("updateTime", updateTime);
        }
        if (area!=null) {
            query.setParameter("area", area);
        }
        List<QhProbabilityEarthquakeDangerPGEntity> list = query.getResultList();
        return list;
    }
}
